package com.itqf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itqf.mapper.PopOrderDataMapper;
import com.itqf.mapper.PopOrderItemMapper;
import com.itqf.pojo.PopOrderData;
import com.itqf.pojo.PopOrderItem;
import com.itqf.service.PopOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2021/1/7
 * @Time: 下午4:33
 */
@Service
public class PopOrderServiceImpl implements PopOrderService {

    @Resource
    private PopOrderDataMapper popOrderDataMapper;

    @Resource
    private PopOrderItemMapper popOrderItemMapper;

    @Resource
    private RestTemplate restTemplate;


    @Transactional
    @LcnTransaction
    @Override
    public int cancelOrder(long orderId,int e) {

        QueryWrapper queryWrapper = new  QueryWrapper();
        queryWrapper.eq("order_id",orderId);
        PopOrderData data = popOrderDataMapper.selectOne(queryWrapper);
        data.setState(6l);

        int i = popOrderDataMapper.update(data,queryWrapper);

        List<PopOrderItem> list = popOrderItemMapper.selectList(queryWrapper);
        for (PopOrderItem item : list) {
            // update    warehouse
            long pId = item.getSkuId();
            long c = item.getNum();
             i +=  restTemplate.
                   getForObject("http://localhost:8081/incr/"+pId+"/"+c,int.class);
        }


        if (e==1){
            int j = 10/0;
        }


        return i;
    }
}
